package com.ruoyi.common.core.utils;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;

import com.aliyun.sdk.service.dysmsapi20170525.models.*;
import com.aliyun.sdk.service.dysmsapi20170525.*;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;
import java.util.concurrent.CompletableFuture;

public class SmsUtils {

    public static void send(String tel,String msg,String signature,String template,int count) throws Exception {

        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId("LTAI5tMNPBcAjiVWCgGhktaM")
                .accessKeySecret("WN6NLMbHg3MPoTJahFdQ3T55pudndo")
                .build());

        AsyncClient client = AsyncClient.builder()
                .region("cn-shanghai") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                )
                .build();
        //.signName("signature")，在最后将双引号删掉，这里为了不浪费短信次数故意写错
        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .signName("signature")
                .templateCode(template)
                .phoneNumbers(tel)
                .templateParam("{\"code\":\""+msg+"\"}")
//                .templateParam("{\"code\":\""+msg+"\"}")
//                .templateParam("{\"code\":\"1234\"}")
                .build();

        // Asynchronously get the return value of the API request
        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
        // Synchronously get the return value of the API request
        SendSmsResponse resp = response.get();
        client.close();

        //重试机制
        if (!StringUtils.equals(resp.getBody().getCode(),"OK")&&count>0){
            count--;
            send(tel,msg,signature,template,count);
        }else {
            System.out.println("重试不成功，联系管理员。。。。。。");
        }

    }

}